/*================================================================
*   Copyright (C) 2021 All rights reserved.
*   
*   文件名称：test_thread.cc
*   创 建 者：汉森伯逸
*   创建日期：2021年04月26日
*   描    述：
*
================================================================*/

#include <vector>

#include "hsby.h"


int count = 0;

hsby::RWMutex rw_mutex;
hsby::Mutex mutex;

void func1(){
//    HSBY_LOG_SYSTEM_DEBUG << "\n   name: " << hsby::Thread::GetThreadName()
//                             << "\n   this.name: " << hsby::Thread::GetThread()->GetThreadName()
//                             << "\n   id: " << hsby::GetThisThreadId()
//                             << "\n   this.id: " << hsby::Thread::GetThread()->GetId();
}

void func2(){
//    HSBY_LOG_SYSTEM_DEBUG << "\n   name: " << hsby::Thread::GetThreadName()
//                             << "\n   this.name: " << hsby::Thread::GetThread()->GetThreadName()
//                             << "\n   id: " << hsby::GetThisThreadId()
//                             << "\n   this.id: " << hsby::Thread::GetThread()->GetId();
    for(int i = 0; i < 100000; ++i) {
        hsby::Mutex::LockGuardType lock(mutex);
        ++count;
    }
}

void func3() {
    while(1) {
        //HSBY_LOG_SYSTEM_DEBUG << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    }
}

void func4() {
    while(1) {
        //HSBY_LOG_SYSTEM_DEBUG << "===============================";
    }
}

void test_thread(){
    //HSBY_LOG_SYSTEM_DEBUG << "test_thread1() begin";

    std::vector<hsby::Thread::Ptr> thrs;
    for(int i = 0; i < 5; ++i){
        hsby::Thread::Ptr thr(new hsby::Thread(func1, "name_" + std::to_string(i)));
        thrs.push_back(thr);
    }
    for(auto& it : thrs){
        it->join();
    }

    //HSBY_LOG_SYSTEM_DEBUG << "test_thread1() end";
}

void test_mutex(){
    //HSBY_LOG_SYSTEM_DEBUG << "test_thread1() begin";

    std::vector<hsby::Thread::Ptr> thrs;
    for(int i = 0; i < 5; ++i){
        hsby::Thread::Ptr thr(new hsby::Thread(func2, "name_" + std::to_string(i)));
        thrs.push_back(thr);
    }
    for(auto& it : thrs){
        it->join();
    }
    //HSBY_LOG_SYSTEM_DEBUG << "count=" << count;
    //HSBY_LOG_SYSTEM_DEBUG << "test_thread1() end";
}

void test_log_mutex() {
    //HSBY_LOG_SYSTEM_DEBUG << "test_log_mutex() end";

    YAML::Node node = YAML::LoadFile("/hshare/Project/scaffold/bin/conf/log_test_mutex.yml");
    hsby::Config::LoadFromYaml(node);

    //HSBY_LOG_SYSTEM_DEBUG << hsby::LoggerManagerSgt::Instance().getLogger("root")->toYaml();
    std::vector<hsby::Thread::Ptr> thrs;
    for(int i = 0; i < 2; ++i){
        hsby::Thread::Ptr thr1(new hsby::Thread(func3, "name_" + std::to_string(i * 2)));
        hsby::Thread::Ptr thr2(new hsby::Thread(func4, "name_" + std::to_string(i * 2 + 1)));
        thrs.push_back(thr1);
        thrs.push_back(thr2);
    }
    for(auto& it : thrs){
        it->join();
    }

    //HSBY_LOG_SYSTEM_DEBUG << "test_log_mutex() end";
}


int main(){
    test_log_mutex();
    return 0;
}
